<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\PermissionRequest;
use Spatie\Permission\Models\Permission;

class PermissionController extends Controller
{
    public function index(){

        $permissions = Permission::orderByDesc('id')->paginate(request('page_size', 10));

        return view('backend.permission.index',compact('permissions'));
    }

    public function create(){

        return view('backend.permission.create');
    }

    public function store(
        PermissionRequest $request,
        Permission $permission
    ){
        $permission->fill($request->filldata())->save();
        flash('权限添加成功', 'success');

        return back();
    }

    public function edit($id)
    {
        $permission = Permission::findOrFail($id);
        return view('backend.permission.edit', compact('permission'));
    }

    public function update(PermissionRequest $request, $id)
    {
        $permission = Permission::findOrFail($id);
        $permission->fill($request->filldata())->save();
        flash('权限编辑成功', 'success');

        return back();
    }

    public function destroy($id)
    {
        $permission = Permission::findOrFail($id);
        //TODO::判断该角色下是否还有管理人员
        //        if ($role->administrators()->exists()) {
        //            flash('该角色下还存在管理员，请先删除相应的管理员');
        //        } else {
        $permission->delete();
        flash('删除成功', 'success');
        //}

        return back();
    }
}
